//
//  HSTopicListVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/4.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSTopicListVC.h"
#import "HSTopicListVM.h"
#import "HSTopicListCell.h"
#import "HSTopicDetailVC.h"
#import "HSPublishTopicVC.h"
#import "HSLoginVC.h"

@interface HSTopicListVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HSTopicListVM *listVM;
@property (nonatomic, strong) HSTopicListCell *tmpCell;

@end

@implementation HSTopicListVC

- (instancetype)initWithName:(NSString *)name slug:(NSString *)slug
{
    if (self = [super init]) {
        _listVM = [[HSTopicListVM alloc] initWithSlug:slug];
        self.title = name;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self rightTitleWithTitle:@"发布"];
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.hintView.reloadBlock();
}

- (void)rightButtonTouch
{
    if (![[NSUserDefaults standardUserDefaults] objectForKey:userNameKey]) {
       return [self my_push:[HSLoginVC new]];
    }
    [self my_push:[[HSPublishTopicVC alloc] initWithTag:self.title]];
}

- (void)refresh
{
    @weakify(self);
    [[self.listVM.dataCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        self.hintView.hintState = self.listVM.forums.count > 0 ? MYHintStateHidden : MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        @strongify(self);
        self.hintView.hintState = MYHintStateNoData;
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

- (void)loadmore
{
    @weakify(self);
    [[self.listVM.loadmoreCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        [self.tableView endRefreshAndLoadMore];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        @strongify(self);
        [self.tableView endRefreshAndLoadMore];
        [UIWindow showError:error];
    }];
}

#pragma mark - uitableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listVM.forums.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSForumAttributesModel *model = self.listVM.forums[indexPath.row].attributes;
    if (model.cellHeight < 1) {
        if (!self.tmpCell) {
            self.tmpCell = [HSTopicListCell new];
        }
        self.tmpCell.model = model;
    }
    return model.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSTopicListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HSTopicListCell class])];
    cell.model = self.listVM.forums[indexPath.row].attributes;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HSForumListModel *model = self.listVM.forums[indexPath.row];
    [self my_push:[[HSTopicDetailVC alloc] initWithTitle:model.attributes.title id:model.id]];
}

#pragma mark - getter


- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = CLEAR_COLOR;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView addRefreshTarget:self action:@selector(refresh)];
        [_tableView addLoadMoreTarget:self action:@selector(loadmore)];
        [_tableView registerClass:[HSTopicListCell class] forCellReuseIdentifier:NSStringFromClass([HSTopicListCell class])];
    }
    return _tableView;
}

@end
